import java.util.Arrays;
import java.util.Scanner;


interface senven {
    void main(String[] args);
}


public class Main {
    public int MoreThanHalfNum_Solution(int [] array) {
        if(array.length == 1) {
            return array[0];
        }


        int ret = array[0];
        int nums = 1;
        for(int i = 1; i < array.length;i++) {
            if(nums > 0) {
                if(array[i] == ret) {
                    nums++;
                }else{
                    nums--;
                }
            }else {
                nums = 1;
                ret = array[i];
            }
        }

        if(nums > 0) {
            return ret;
        }
        return 0;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        String str = String.valueOf(a);
        int b = scanner.nextInt();
        System.out.println(Integer.parseInt(str,b));
    }
    public static void Swap(char[] array,int end,int len) {
        while (end < len) {
            char tmp = array[end];
            array[end] = array[len];
            array[len] = tmp;
            end++;
            len--;
        }
    }

    public static void main1(String[] args) {
        Scanner scan = new Scanner(System.in);
        String str = scan.nextLine();
        char[] array = str.toCharArray();

        int i = 0;
        int j = 0;
        while(i < str.length()) {
            j = i;
            while(i < str.length() && str.charAt(i) != ' '){
                i++;
            }

            Swap(array,j,i-1);
            i++;
        }
        Swap(array,0, array.length-1);
        String str1 = new String(array);
        System.out.println(str1);
    }
}
